function keywordHighlight(idHtmlContent,keyword) {

var content= $("#"+idHtmlContent).html();//获取内容
if ($.trim(keyword)==""){
    return;//关键字为空则返回
}
var htmlReg = new RegExp("\<.*?\>", "i");
var arrA = new Array();
//替换HTML标签
for (var i = 0; true; i++) {
    var m = htmlReg.exec(content);
    if (m) {
        arrA[i] = m;
    }else {
        break;
    }
    content = content.replace(m, "{[(" + i + ")]}");
}
words = unescape(keyword.replace(/\+/g, ' ')).split(/\s+/);
//替换关键字 
for (w = 0; w < words.length; w++) {
    var r = new RegExp("(" + words[w].replace(/[(){}.+*?^$|\\\[\]]/g, "\\$&") + ")", "ig");
    content = content.replace(r, "<b><span style='color:red;font-size:14px;'><u>"+words[w]+"</u></span></b>");//关键字样式
}
//恢复HTML标签 
for (var i = 0; i < arrA.length; i++) {
    content = content.replace("{[(" + i + ")]}", arrA[i]);
}
 $("#"+idHtmlContent).html(content);

}


韩韩
7 声望0 粉丝

好好学习,天天向上


« 上一篇
vue